using BasicScript.Component;
using Game.Core;
using Game.Main.Model;
using Game.Manager;
using UnityEngine;

namespace Game.Component
{
	public class POSItem : BaseItem
	{
		private const int MIN_NUM = 1;

		private const int MAX_NUM = 1000;

		private int num;

		private Transform numTrans;

		private SpriteAssets spriteAssets;

		private SpriteRenderer[] numRenders;

		private bool isPlayAnim;

		private int animStage;

		private float animTimer;

		private int originalNum;

		public override void Init(ItemArchiveData data)
		{
			base.Init(data);
			numTrans = model.Find("Num");
			spriteAssets = model.GetComponent<SpriteAssets>();
		}

		protected override void LateUpdate()
		{
			base.LateUpdate();
			if (!isPlayAnim)
			{
				return;
			}
			if (animStage == 0)
			{
				animTimer += Time.deltaTime * 4f;
				if (animTimer >= 1f)
				{
					animStage = 1;
					animTimer = 0f;
				}
				return;
			}
			animTimer += Time.deltaTime;
			if (animTimer >= 1f)
			{
				SetNumber(0, numTrans);
				animTimer = 0f;
				animStage = 0;
				isPlayAnim = false;
			}
			else
			{
				SetNumber((int)Mathf.Lerp(originalNum, 0f, animTimer), numTrans);
			}
		}

		public override void QuicklyTouch(TouchEventData touchEventData)
		{
			base.QuicklyTouch(touchEventData);
			Singleton<AudioManager>.Instance.PlaySound("sound_btn_1");
			SetNumber(Random.Range(1, 1000), numTrans);
		}

		public void SwipingCard()
		{
			if (!isPlayAnim)
			{
				isPlayAnim = true;
				animStage = 0;
				animTimer = 0f;
				originalNum = num;
				Singleton<AudioManager>.Instance.PlaySound("sound_pos");
			}
		}

		public void SetNumber(int val, Transform numTrans)
		{
			num = Mathf.Clamp(val, 0, 999);
			string[] array = new string[3];
			if (num < 10)
			{
				array[0] = "0";
				array[1] = "0";
				array[2] = num.ToString();
			}
			else if (num < 100)
			{
				array[0] = "0";
				array[1] = num.ToString().Substring(0, 1);
				array[2] = num.ToString().Substring(1, 1);
			}
			else
			{
				array[0] = num.ToString().Substring(0, 1);
				array[1] = num.ToString().Substring(1, 1);
				array[2] = num.ToString().Substring(2, 1);
			}
			if (numRenders == null)
			{
				numRenders = new SpriteRenderer[3];
				numRenders[0] = numTrans.Find("0").GetComponent<SpriteRenderer>();
				numRenders[1] = numTrans.Find("1").GetComponent<SpriteRenderer>();
				numRenders[2] = numTrans.Find("2").GetComponent<SpriteRenderer>();
			}
			for (int i = 0; i < array.Length; i++)
			{
				string text = array[i];
				numRenders[i].sprite = spriteAssets.GetSprite(text);
			}
		}
	}
}
